﻿using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Extensions.Logging;

namespace Dan.Common
{
    /// <summary>
    /// 性能助手,查看方法执行时间
    /// </summary>
    public static class StopwatchExtend
    {
        /// <summary>
        /// 扩展logger 显示方法的执行时间
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="logLevel"></param>
        /// <param name="action"></param>
        public static void LogTime(this ILogger logger, LogLevel logLevel, Action action)
        {
            Stopwatch sw = Stopwatch.StartNew();
            action();
            sw.Stop();

            logger.Log(logLevel, (sw.ElapsedMilliseconds / 1000.0).ToString(CultureInfo.InvariantCulture));
        }
    }
}